<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



ini_set('max_execution_time', 3600);
ini_set('memory_limit', '128M');

include_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/engine/start.php');
include_once(dirname(dirname(__FILE__)) . '/_init.php');
//die(test_include());

function remove_n($str) {
    $str = mysql_real_escape_string($str);
    $len = strlen($str);
    return substr($str, 0, $len - 2);
}

$start = get_input('s');
$end = $start * 10 * 8 + 10 * 8;
$start = $start * 10 * 8;
//$end = $end * 8;
//die($start . ',' . $end);

echo "---USERS---<br>";
$start = 0;
$whdu = file('whiteboard_hd_user');
$len = count($whdu);
$end = $len * 8;
//$mapping = array();
for($index = $start; $index < $len && $index < $end; $index+=8) {
    $guid = remove_n($whdu[$index+0]);
    $name = remove_n($whdu[$index+1]);
    //echo "$name<br>";
    $username = $whdu[$index+2];
    $busername = remove_n($username);
    $query = "UPDATE 3lGG_users_entity SET username = '$busername' WHERE username = '$username'";
    echo "$query<br>";
    echo update_data($query) . '<br>';
    $password = remove_n($whdu[$index+3]);
    $email = remove_n($whdu[$index+4]);
    $salt = remove_n($whdu[$index+5]);
    $login = remove_n($whdu[$index+6]);
    $domain = remove_n($whdu[$index+7]);
    $query = "UPDATE 3lGG_users_entity SET name = '$name', email = '$email', salt = '$salt', password = '$password' WHERE username = '$busername'";
    echo "$query<br>";
    echo update_data($query) . '<br>';
    continue;
    $userEntity = get_user_by_username($username);
    if ($userEntity) {
        echo '<br>' . $username . ' already exists';
	//$userEntity->setMetaData('isWPUser', 1);
        $userEntity->clearMetaData(WHITEBOARD);
        $userEntity->setMetadata(WHITEBOARD, 1);
        $userEntity->salt = $salt;
        $userEntity->password = mysql_real_escape_string($password);
        $userEntity->save();
        //$mapping[$username] = $userEntity->guid;
	greengar_whiteboard_add_xmppAccount($login, $domain, $userEntity->guid);
        continue;
    } 
    // WHAT TO DO??? THIS SHOULD NOT HAPPEN
    $userEntity = get_user_by_email($email);
    if ($userEntity && is_array($userEntity)) {
        $userEntity = array_shift($userEntity);
        if ($userEntity) {
            echo '<br>' . $email . ' already exists';
            //$mapping[$username] = $userEntity->guid;
	    //$userEntity->setMetaData('isWPUser', 1);
       	    $userEntity->clearMetaData(WHITEBOARD);
            $userEntity->setMetadata(WHITEBOARD, 1);
            $userEntity->salt = $salt;
            $userEntity->password = mysql_real_escape_string($password);
            $userEntity->save();
            //$mapping[$username] = $userEntity->guid;
	    greengar_whiteboard_add_xmppAccount($login, $domain, $userEntity->guid);
            continue;
        }
    }
    // WHAT TO DO??? THIS SHOULD NOT HAPPEN
    $lastname = $name;
    // DONT SEND EMAIL TO THESE OLD USER;
    $try = greengar_register($email, $username, $password, $firstname, $lastname, FALSE);

    if ($try) {
        $status = $try['status'];
        $message = $try['result'];
        if ($status == 200) {
            // Successfully create user, verify that
            echo '<br>' . $email . ' is created successfully';
            $userid = $message;
            $userEntity = get_entity($userid);
            if ($userEntity) {
                // Set password for this one
                $userEntity->password = mysql_real_escape_string($password);

                $userEntity->setMetaData('isWPUser', 1);
                $userEntity->clearMetaData(WHITEBOARD);
                $userEntity->setMetadata(WHITEBOARD, 1);
                //$userEntity->save();
                $userEntity->save();
                //$mapping[$username] = $userid;
                //$userEntity = get_entity($userid);
		greengar_whiteboard_add_xmppAccount($login, $domain, $userEntity->guid);
                echo '<br> in database password is ' . $userEntity->password;
                echo '<br> change ' . $userid . ' s passowrd to ' . $password;
            } else {
                // Fail to set password for this one
                echo '<br> failed to change ' . $userid . ' s passowrd to ' . $password;
            }
        } else {
            // What to do ???
            echo '<br>' . $email . ' is failed to create : ' . $message;
        }
    } else {
        echo '<br>' . $email . ' is failed to create : unexpected error happened';
    }
    //break;
	
}

/*
$whdr = file('whiteboard_hd_relationship');
echo "---RELAS---<br>";
$len = count($whdr);
for($index = 0; $index < $len; $index+=2) {
    $user1 = $whdr[$index+0];
    //$relationship = $datum->name;
    $user2 = $whdr[$index+1];
    echo "$user1,$user2<br>";
    $user_id1 = get_user_by_username($user1);
    $user_id2 = get_user_by_username($user2);
    if($user_id1 && $user_id2)
        echo greengar_add_relationship($user_id1->guid, $user_id2->guid, WHITEBOARD) . "<br>";
    //break;
}
*/
?>
